<template>
  <div class="article-page">
    <!-- 筛选区域 -->
    <el-card>
      <div slot="header">
        <!-- 面包屑 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 表单 -->
      <el-form>
        <el-form-item label="状态 :">
           <el-radio-group v-model="reqParams.status">
             <el-radio :label="null">全部</el-radio>
             <el-radio :label="0">草稿</el-radio>
             <el-radio :label="1">待审核</el-radio>
             <el-radio :label="2">审核通过</el-radio>
             <el-radio :label="3">审核失败</el-radio>
             <el-radio :label="4">已删除</el-radio>
        </el-radio-group>
        </el-form-item>
        <el-form-item label="频道 :">
          <!-- <el-select v-model="reqParams.channel_id" clearable placeholder="请选择">
             <el-option
                v-for="item in channelOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id">
             </el-option>
          </el-select> -->
          <MyChannel :value="reqParams.channel_id"
          @input="reqParams.channel_id=$event"
          ></MyChannel>
        </el-form-item>
        <el-form-item label="日期 :">
           <el-date-picker
             v-model="dataArry"
             type="daterange"
             range-separator="至"
             start-placeholder="开始日期"
             end-placeholder="结束日期"
             @change="changeDate"
             >
           </el-date-picker>
        </el-form-item>
        <el-form-item label="">
          <el-button type="primary" @click="filterArticle">筛选</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 结果区域 -->
    <el-card style="margin-top:20px">
      <div slot="header">根据筛选条件共查到{{total}}条结果 :</div>
      <!-- 表格 -->
      <el-table :data="articles">
        <el-table-column label="封面">
          <template slot-scope="scope">
            <el-image :src="scope.row.cover.images[0]" style="width:180px;height:120px">
             <div slot="error" >
             <img src="../../assets/error.png" style="width:150px,height:150px">
            </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="标题" prop="title" width="400px"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status===0" type="info">草稿</el-tag>
            <el-tag v-if="scope.row.status===1">待审核</el-tag>
            <el-tag v-if="scope.row.status===2" type="success">审核通过</el-tag>
            <el-tag v-if="scope.row.status===3" type="warning">审核失败</el-tag>
            <el-tag v-if="scope.row.status===4" type="danger">已删除</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="发布时间" prop="pubdate" ></el-table-column>
        <el-table-column label="阅读数" prop="read_count" width="120px"></el-table-column>
        <el-table-column label="评论数" prop="comment_count" width="120px"></el-table-column>
        <el-table-column label="点赞数" prop="like_count" width="120px"></el-table-column>
        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
             <el-button type="primary" icon="el-icon-edit" circle plain
             @click="$router.push(`/publish?id=${scope.row.id}`)"></el-button>
              <el-button type="danger" icon="el-icon-delete" circle plain @click="delArticle(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
         background
         layout="prev, pager, next"
         :total="total"
         :current-page="reqParams.page"
         :page-size="reqParams.per_page"
         style="margin-top:20px"
         @current-change="changePager"
         >
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  name: 'Article-page',
  data () {
    return {
      reqParams: {
        status: null,
        channel_id: null,
        begin_pubdate: null,
        end_pubdate: null,
        page: 1,
        per_page: 10
      },
      // 频道选项
      // channelOptions: [],
      dataArry: [],
      articles: [],
      total: 0
    }
  },
  created () {
    // this.getChannelOptions()
    this.getArticle()
  },
  methods: {
    // async getChannelOptions () {
    //   const res = await this.$http.get('/v1_0/channels')
    //   // console.log(res.data.data.channels)
    //   this.channelOptions = res.data.data.channels
    // },
    async getArticle () {
      const res = await this.$http.get('/v1_0/mp/articles', { params: this.reqParams })
      console.log(res.data)
      this.articles = res.data.data.results
      this.total = res.data.data.total_count
    },
    // 分页功能实现
    changePager (newPage) {
      this.reqParams.page = newPage
      this.getArticle()
    },
    // 筛选文章
    filterArticle () {
      // 做筛选的时候从第一页开始
      this.reqParams.page = 1
      this.getArticle()
    },
    // 选择日期筛选功能实现
    changeDate (dataArr) {
      // console.log(dataArr)
      // 当你清空的时候,dataArr是null,选择日期后dataArr=[起始时间,结束时间]
      if (dataArr) {
        this.reqParams.begin_pubdate = dataArr[0]
        this.reqParams.end_pubdate = dataArr[1]
      } else {
        this.reqParams.begin_pubdate = null
        this.reqParams.end_pubdate = null
      }
    },
    // 删除文章功能实现
    delArticle (id) {
      this.$confirm('此操作将永久删除该文章, 是否继续?', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 发送删除请求
        await this.$http.delete(`/v1_0/mp/articles/${id}`)
        // 成功提示
        this.$message.success('删除成功')
        // 重新渲染页面
        this.getArticle()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
<style scoped lang="less"></style>
